package org.jeecg.modules.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysLoginServiceImpl implements ISysLoginService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public JSONObject tokenInfo(SysUser sysUser) {
        String password = sysUser.getPassword();
        String username = sysUser.getUsername();
        JSONObject obj = new JSONObject();
        String token = JwtUtil.sign(username, password);
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
        obj.put("token", token);
        obj.put("expire", redisUtil.getExpire(CommonConstant.PREFIX_USER_TOKEN + token));
        return obj;
    }
}
